﻿using System;
using System.Collections.Generic;
using HslCommunication.Core;

namespace HslCommunication.BasicFramework
{
	/// <summary>
	/// 一个高效的数组管理类，用于高效控制固定长度的数组实现<br />
	/// An efficient array management class for efficient control of fixed-length array implementations
	/// </summary>
	/// <typeparam name="T">泛型类型</typeparam>
	// Token: 0x02000184 RID: 388
	public class SharpList<T>
	{
		/// <summary>
		/// 实例化一个对象，需要指定数组的最大数据对象
		/// </summary>
		/// <param name="count">数据的个数</param>
		/// <param name="appendLast">是否从最后一个数添加</param>
		// Token: 0x06001FAD RID: 8109 RVA: 0x00099950 File Offset: 0x00097B50
		public SharpList(int count, bool appendLast = false)
		{
			bool flag = count > 8192;
			if (flag)
			{
				this.capacity = 4096;
			}
			this.array = new T[this.capacity + count];
			this.hybirdLock = new SimpleHybirdLock();
			this.count = count;
			if (appendLast)
			{
				this.lastIndex = count;
			}
		}

		/// <summary>
		/// 获取数组的个数<br />
		/// Get the number of arrays
		/// </summary>
		// Token: 0x1700068F RID: 1679
		// (get) Token: 0x06001FAE RID: 8110 RVA: 0x000999C6 File Offset: 0x00097BC6
		public int Count
		{
			get
			{
				return this.count;
			}
		}

		/// <summary>
		/// 新增一个数据值<br />
		/// Add a data value
		/// </summary>
		/// <param name="value">数据值</param>
		// Token: 0x06001FAF RID: 8111 RVA: 0x000999D0 File Offset: 0x00097BD0
		public void Add(T value)
		{
			this.hybirdLock.Enter();
			bool flag = this.lastIndex < this.capacity + this.count;
			if (flag)
			{
				T[] array = this.array;
				int num = this.lastIndex;
				this.lastIndex = num + 1;
				array[num] = value;
			}
			else
			{
				T[] destinationArray = new T[this.capacity + this.count];
				Array.Copy(this.array, this.capacity, destinationArray, 0, this.count);
				this.array = destinationArray;
				this.lastIndex = this.count;
			}
			this.hybirdLock.Leave();
		}

		/// <summary>
		/// 批量的增加数据<br />
		/// Increase data in batches
		/// </summary>
		/// <param name="values">批量数据信息</param>
		// Token: 0x06001FB0 RID: 8112 RVA: 0x00099A74 File Offset: 0x00097C74
		public void Add(IEnumerable<T> values)
		{
			foreach (T value in values)
			{
				this.Add(value);
			}
		}

		/// <summary>
		/// 获取数据的数组值<br />
		/// Get array value of data
		/// </summary>
		/// <returns>数组值</returns>
		// Token: 0x06001FB1 RID: 8113 RVA: 0x00099AC4 File Offset: 0x00097CC4
		public T[] ToArray()
		{
			this.hybirdLock.Enter();
			bool flag = this.lastIndex < this.count;
			T[] array;
			if (flag)
			{
				array = new T[this.lastIndex];
				Array.Copy(this.array, 0, array, 0, this.lastIndex);
			}
			else
			{
				array = new T[this.count];
				Array.Copy(this.array, this.lastIndex - this.count, array, 0, this.count);
			}
			this.hybirdLock.Leave();
			return array;
		}

		/// <summary>
		/// 获取或设置指定索引的位置的数据<br />
		/// Gets or sets the data at the specified index
		/// </summary>
		/// <param name="index">索引位置</param>
		/// <returns>数据值</returns>
		// Token: 0x17000690 RID: 1680
		public T this[int index]
		{
			get
			{
				bool flag = index < 0;
				if (flag)
				{
					throw new IndexOutOfRangeException("Index must larger than zero");
				}
				bool flag2 = index >= this.count;
				if (flag2)
				{
					throw new IndexOutOfRangeException("Index must smaller than array length");
				}
				T result = default(T);
				this.hybirdLock.Enter();
				bool flag3 = this.lastIndex < this.count;
				if (flag3)
				{
					result = this.array[index];
				}
				else
				{
					result = this.array[index + this.lastIndex - this.count];
				}
				this.hybirdLock.Leave();
				return result;
			}
			set
			{
				bool flag = index < 0;
				if (flag)
				{
					throw new IndexOutOfRangeException("Index must larger than zero");
				}
				bool flag2 = index >= this.count;
				if (flag2)
				{
					throw new IndexOutOfRangeException("Index must smaller than array length");
				}
				this.hybirdLock.Enter();
				bool flag3 = this.lastIndex < this.count;
				if (flag3)
				{
					this.array[index] = value;
				}
				else
				{
					this.array[index + this.lastIndex - this.count] = value;
				}
				this.hybirdLock.Leave();
			}
		}

		/// <inheritdoc />
		// Token: 0x06001FB4 RID: 8116 RVA: 0x00099C90 File Offset: 0x00097E90
		public override string ToString()
		{
			return string.Format("SharpList<{0}>[{1}]", typeof(T), this.capacity);
		}

		// Token: 0x04000759 RID: 1881
		private T[] array;

		// Token: 0x0400075A RID: 1882
		private int capacity = 2048;

		// Token: 0x0400075B RID: 1883
		private int count = 0;

		// Token: 0x0400075C RID: 1884
		private int lastIndex = 0;

		// Token: 0x0400075D RID: 1885
		private SimpleHybirdLock hybirdLock;
	}
}
